import {getDicts, listAll} from '@/api/system/dict/data'
import {handleTree} from '@/utils/ruoyi'

export default {
  async loadDictByType({state, commit, dispatch}, {dictType, dictKind}) {

    if(state.dictType[dictType] && state.dictType[dictType].length > 0) {
      return state.dictType[dictType]
    }

    const res = await listAll({dictType})
    const data = res.data || []

    let list = data.map(o => {
      return {
        ...o,
        id: o.dictCode,
        label: o.dictLabel,
        value: o.dictValue,
        parentId: o.parentId
      }
    })
    if(dictKind === 2) {
      list = handleTree(list)
    }
    commit('dictType/set_dict_type', {type: dictType, list})

    return list
  }
}
